package com.community.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.community.annotation.SysLog;
import com.community.base.BaseService;
import com.community.common.Const;
import com.community.pojo.EventRecord;
import com.community.service.IJedisClientService;
import com.community.service.IMessageProcessService;
import com.community.util.NumberUtil;
import com.community.util.PropertiesUtil;
import com.community.util.StringUtil;
import com.community.vo.RemindMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 消息处理服务层
 */
@Service("iMessageProcessService")
public class MessageProcessServiceImpl extends BaseService implements IMessageProcessService{

    @Autowired
    private IJedisClientService iJedisClientService;

    /**
     * 事件处理
     */
    @SysLog("事件处理")
    public void remindProcess(){
        //查询最新事件记录，获取上次事件信息
        Map eventMap = eventRecordMapper.selectMaxIdData();
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        //查询截止至当前的生效事件
        if(eventMap == null){
            //没有事件处理记录
            eventMap.put("maxTime",date);
        }
        eventMap.put("orderBy",Const.YesOrNo.YES);
        //不查询公告
        eventMap.put("messageType",Const.YesOrNo.YES);
        List<RemindMessage> resultList = remindMapper.selectByParam(eventMap);
        if(null != resultList && 0 < resultList.size()){
            //更新事件为失效状态
            remindMapper.updateByParam(eventMap);
            //保存事件记录
            EventRecord record = new EventRecord();
            record.setMaxId(NumberUtil.safeToNumber(resultList.get(0).getId()));
            record.setTimes(date);
            record.setNums(resultList.size());
            eventRecordMapper.insert(record);
            //对事件进行分组合并，接收用户分组
            Map<String,List<RemindMessage>> userGroupMap = resultList.stream().collect(Collectors.groupingBy(RemindMessage::getBeUser));
            Set<String> stringSet = userGroupMap.keySet();
            for (String s : stringSet){
                List<RemindMessage> remindMessageList = userGroupMap.get(s);
                if(null != remindMessageList){
                    //产生消息
                    for (int i = 0; i < remindMessageList.size(); i++) {
                        //合成消息
                        String beUser = StringUtil.valueOfString(remindMessageList.get(i).getBeUser());//被提醒者
                        String eventUser = StringUtil.valueOfString(remindMessageList.get(i).getEventUser());//事件产生者
                        if(beUser.equals(eventUser)){//如果是自己回答了自己的提问
                            continue;
                        }
                        //作用对象
                        Integer target = NumberUtil.safeToNumber(remindMessageList.get(i).getTarget());
                        Map templateMap = null;//模板集合
                        if(target == Const.RemindTarget.ONE){//提问
                            templateMap = messageTemplateMapper.getByCode(PropertiesUtil.getProperty("templatecodeone"));
                        }else if(target == Const.RemindTarget.TWO){//回答
                            templateMap = messageTemplateMapper.getByCode(PropertiesUtil.getProperty("templatecodetwo"));
                        }else if(target == Const.RemindTarget.THREE){//评论
                            templateMap = messageTemplateMapper.getByCode(PropertiesUtil.getProperty("templatecodethree"));
                        }
                        if(templateMap != null){
                            String content = StringUtil.valueOfString(templateMap.get("content"));//模板内容
                            String euname = StringUtil.valueOfString(remindMessageList.get(i).getEuname());//产生者昵称
                            String title = StringUtil.valueOfString(remindMessageList.get(i).getTitle());//问题标签
                            //替换内容
                            content.replaceAll("[${eventUser}]",euname);
                            content.replaceAll("[${questionName}]",title);
                            remindMessageList.get(i).setContent(content);//消息内容
                            //存入redis
                            iJedisClientService.set(Const.MESSAGE+remindMessageList.get(i).getBeUser(),
                                    JSONObject.toJSONString(remindMessageList));
                        }
                    }
                }
            }
        }
    }

}
